package com.amumu.drama.common.commpont;

import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;

import java.util.concurrent.TimeUnit;

/**
 * @author sunzhuang
 * @date 2024/9/12 15:41
 * @slogan 天下风云出我辈, 一入代码岁月催
 * @desc todo
 */

public class LrCacheManager {

    public static final Long DAY_SECONDS = 60 * 60 * 24L;

    public static final Long HOUR_SECONDS = 60 * 60L ;


    public static <K1, V1> LoadingCache<K1, V1> getCache(CacheLoader<? super K1, V1> loader) {
        return Caffeine.newBuilder()
                .expireAfterWrite(3 * 60 * 60, TimeUnit.SECONDS)
                .initialCapacity(100).maximumSize(100)
                .build(loader);
    }

    public static <K1, V1> LoadingCache<K1, V1> getCache(Long expireSeconds, CacheLoader<? super K1, V1> loader) {
        return Caffeine.newBuilder()
                .expireAfterWrite(expireSeconds, TimeUnit.SECONDS)
                .initialCapacity(100)
                .maximumSize(100)
                .build(loader);
    }

    public static <K1, V1> LoadingCache<K1, V1> getCache(Long expireSeconds, int initialCapacity, int maxSize, CacheLoader<? super K1, V1> loader) {
        return Caffeine.newBuilder()
                .expireAfterWrite(expireSeconds, TimeUnit.SECONDS)
                .initialCapacity(initialCapacity)
                .maximumSize(maxSize)
                .build(loader);
    }
}
